<template>
  <a-input v-bind="$attrs" @click="openFn" v-model:value="txtValue" :disabled="props.isAdd && isUpdate"
    placeholder="请选择人员">
    <template #suffix>
      <CloseCircleOutlined @click="clearFn" />

    </template>
  </a-input>
  <!--  <a-button type="primary" style="margin-top: 10px" v-if="props.isAdd&&isUpdate">添加人员</a-button>-->
  <AccountList @register="registerModal" @success="handleSuccess" :mode="props.mode"></AccountList>
</template>

<script lang="ts" setup>
import AccountList from './index.vue';
import { useModal } from "@/components/Modal";
import { ref, computed, watch,useAttrs } from "vue";
import { userListsApi } from '@/api/sys/user';
import { useMessage } from "@/hooks/web/useMessage";
import { CloseCircleOutlined } from '@ant-design/icons-vue';

import { useUserStore } from '@/store/modules/user';
const userStore = useUserStore();
const attrs = useAttrs();


defineOptions({ name: 'Ainput' });
const props = defineProps({
  mode: { type: String, default: "multiple" },
  ids: { type: String, default: "" },
  isAdd: { type: Boolean, default: false },
  value: { type: String, default: "" },
});
const emit = defineEmits(['change', 'update:value', "success"]);
const [registerModal, { openModal }] = useModal();
const { createMessage } = useMessage();
//userStore.chatIsUpdate
const isUpdate = computed(() => { return userStore.chatIsUpdate });
const options = ref([])
// const txtValue = computed(() => { return props.value });
const txtValue = ref("");


watch(() => props.value, (val) => {
  // console.log("--props.value:",attrs)
  txtValue.value = val

})

function openFn() {
  // console.log("---props.mode222",props.mode)
  //console.log("--ids::",props.ids)
  openModal(true, { ids: props.ids })
}

function clearFn(e) {
  txtValue.value = "";
  let result = { ids: "", names: "" };
  emit("success", result);
}


function handleSuccess(list) {


  let ids = [];

  // let option=[];
  list.forEach(e => {
    let json = {
      value: e.userId,
      label: e.userName
    };
    //  names.push(e.userName)
    // option.push(json);
    ids.push(e.userId)
  });
  let ids_str = ids.join(",");
  userListsApi({ userIds: ids_str }).then(res => {
    if (res.code == 200) {
      let data_arr = res.data;
      let names = [];

      data_arr.forEach(r => {
        names.push(r.userName)
      });
      let result = { ids: ids_str, names: names.join(",") };
      // console.log("--result:", result)
      emit("success", result)
      // console.log("---handleSuccess",names)
    } else {
      createMessage.error(res.message);
    }
  })
  // options.value=option;

}
function onLoadDataFn(ids) {
  // console.log("---接收数据:",ids)
}
</script>

<style scoped></style>
